using System;
using System.Windows.Forms;
using System.Drawing;
using System.Data;

namespace HIS
{
    public class WorkloadForm : Form
    {
        private ComboBox cmbDepartment;
        private ComboBox cmbDoctor;
        private DataGridView dgvStatistics;
        private DateTimePicker dtpStart;
        private DateTimePicker dtpEnd;
        private Button btnQuery;
        private Button btnExport;

        public WorkloadForm()
        {
            InitializeComponents();
            InitializeEvents();
        }

        private void InitializeComponents()
        {
            // 窗体设置
            this.Text = "门诊工作量统计";
            this.Size = new Size(800, 600);
            this.StartPosition = FormStartPosition.CenterScreen;

            // 选择区域
            GroupBox gbSelection = new GroupBox
            {
                Text = "选择条件",
                Location = new Point(10, 10),
                Size = new Size(780, 60)
            };

            // 科室选择
            Label lblDepartment = new Label
            {
                Text = "科室：",
                Location = new Point(10, 25),
                AutoSize = true
            };

            cmbDepartment = new ComboBox
            {
                Location = new Point(60, 22),
                Size = new Size(200, 25),
                DropDownStyle = ComboBoxStyle.DropDownList
            };

            // 医生选择
            Label lblDoctor = new Label
            {
                Text = "医生：",
                Location = new Point(270, 25),
                AutoSize = true
            };

            cmbDoctor = new ComboBox
            {
                Location = new Point(320, 22),
                Size = new Size(200, 25),
                DropDownStyle = ComboBoxStyle.DropDownList
            };

            // 添加时间选择控件
            Label lblStartDate = new Label
            {
                Text = "开始日期：",
                Location = new Point(530, 25),
                AutoSize = true
            };

            dtpStart = new DateTimePicker
            {
                Location = new Point(600, 22),
                Size = new Size(80, 25),
                Format = DateTimePickerFormat.Short
            };

            Label lblEndDate = new Label
            {
                Text = "结束日期：",
                Location = new Point(690, 25),
                AutoSize = true
            };

            dtpEnd = new DateTimePicker
            {
                Location = new Point(760, 22),
                Size = new Size(80, 25),
                Format = DateTimePickerFormat.Short
            };

            // 添加查询和导出按钮
            btnQuery = new Button
            {
                Text = "查询",
                Location = new Point(10, 520),
                Size = new Size(80, 30)
            };

            btnExport = new Button
            {
                Text = "导出",
                Location = new Point(100, 520),
                Size = new Size(80, 30)
            };

            gbSelection.Controls.AddRange(new Control[] {
                lblDepartment,
                cmbDepartment,
                lblDoctor,
                cmbDoctor,
                lblStartDate,
                dtpStart,
                lblEndDate,
                dtpEnd
            });

            // 统计结果显示区域
            GroupBox gbStatistics = new GroupBox
            {
                Text = "统计结果",
                Location = new Point(10, 80),
                Size = new Size(780, 500)
            };

            dgvStatistics = new DataGridView
            {
                Location = new Point(10, 25),
                Size = new Size(760, 465),
                AllowUserToAddRows = false,
                AllowUserToDeleteRows = false,
                ReadOnly = true,
                SelectionMode = DataGridViewSelectionMode.FullRowSelect
            };

            // 添加列
            dgvStatistics.Columns.AddRange(new DataGridViewColumn[] {
                new DataGridViewTextBoxColumn { Name = "日期", HeaderText = "日期", Width = 100 },
                new DataGridViewTextBoxColumn { Name = "挂号人次", HeaderText = "挂号人次", Width = 100 },
                new DataGridViewTextBoxColumn { Name = "接诊人次", HeaderText = "接诊人次", Width = 100 },
                new DataGridViewTextBoxColumn { Name = "金额", HeaderText = "金额", Width = 100 },
                new DataGridViewTextBoxColumn { Name = "诊断信息", HeaderText = "诊断信息", Width = 360 }
            });

            gbStatistics.Controls.Add(dgvStatistics);

            // 添加控件到窗体
            this.Controls.AddRange(new Control[] {
                gbSelection,
                gbStatistics,
                btnQuery,
                btnExport
            });
        }

        private void InitializeEvents()
        {
            // 科室选择变化时更新医生列表
            cmbDepartment.SelectedIndexChanged += (s, e) =>
            {
                LoadDoctors(cmbDepartment.SelectedValue?.ToString());
            };

            // 查询按钮点击事件
            btnQuery.Click += (s, e) =>
            {
                QueryWorkload();
            };

            // 导出按钮点击事件
            btnExport.Click += (s, e) =>
            {
                ExportToExcel();
            };
        }

        private void LoadDoctors(string departmentId)
        {
            cmbDoctor.Items.Clear();
            // TODO: 从数据库加载医生列表
            // 示例代码：
            // var doctors = Database.GetDoctorsByDepartment(departmentId);
            // foreach (var doctor in doctors)
            // {
            //     cmbDoctor.Items.Add(new { Id = doctor.Id, Name = doctor.Name });
            // }
        }

        private void QueryWorkload()
        {
            dgvStatistics.Rows.Clear();
            // TODO: 从数据库查询工作量数据
            // 示例代码：
            // var workload = Database.GetWorkload(
            //     cmbDepartment.SelectedValue?.ToString(),
            //     cmbDoctor.SelectedValue?.ToString(),
            //     dtpStart.Value,
            //     dtpEnd.Value
            // );
            // foreach (var record in workload)
            // {
            //     dgvStatistics.Rows.Add(
            //         record.Date,
            //         record.RegistrationCount,
            //         record.TreatmentCount,
            //         record.Amount,
            //         record.Diagnosis
            //     );
            // }
        }

        private void ExportToExcel()
        {
            // TODO: 实现导出到Excel功能
            // 示例代码：
            // using (SaveFileDialog sfd = new SaveFileDialog())
            // {
            //     sfd.Filter = "Excel Files|*.xlsx";
            //     if (sfd.ShowDialog() == DialogResult.OK)
            //     {
            //         ExcelExporter.ExportWorkload(dgvStatistics, sfd.FileName);
            //     }
            // }
        }
    }
} 